package com.baosight.sczx.common.zm.cInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;

/**
 * 
 * <p>Title: User.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2010-12-16 下午04:43:26  严洪浩  Create
 *</pre>
 */
public class User {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	private static User instance = new User();
	
	@SuppressWarnings("rawtypes")
	private static List<Map> _list;
	
	private User(){
		initData();//当第一次使用的时候初始化
	}
	
	/**
	 * 初始化数据
	 * <p>Function: initData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-9上午05:41:20 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initData() {
		if(_list != null) {
			_list = null;
		}
		_list = new ArrayList<Map>();
		try {
			_list = dao.query("ZM03.queryInterface", null);
		} catch (Exception e) {
			System.out.println(User.class.getName() + "获取所有用户异常：" + e.getMessage());
		}
	}
	
	/**
	 * 根据ID获取用户名称
	 * <p>Function: getNameById</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-16下午04:52:48 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public String getNameById(String userID) {
		for(int i = 0; i < _list.size(); i++) {
			Map _map = _list.get(i);
			
			String key = _map.get("empCode") == null ? "" : _map.get("empCode").toString();
			if(key.equals(userID)) {
				return _map.get("empName").toString();
			}
		}
		
		return null;
	}
	
	/**
	 * 根据人员ID
	 * 获得该人员的机构ID
	 * <p>Function: getDepartmentCodeById</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-10上午04:45:02 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public String getDepartmentCodeById(String userID) {
		for(int i = 0; i < _list.size(); i++) {
			Map _map = _list.get(i);
			
			String key = _map.get("empCode") == null ? "" : _map.get("empCode").toString();
			if(key.equals(userID)) {
				return _map.get("orgCode").toString();
			}
		}
		
		return null;
	}
	
	/**
	 * 根据人员ID
	 * 获得该人员的机构名称
	 * <p>Function: getDepartmentNameById</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-10上午04:46:12 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public String getDepartmentNameById(String userID) {
		for(int i = 0; i < _list.size(); i++) {
			Map _map = _list.get(i);
			
			String key = _map.get("empCode") == null ? "" : _map.get("empCode").toString();
			if(key.equals(userID)) {
				return _map.get("orgName").toString();
			}
		}
		
		return null;
	}
	
	/**
	 * 获取所有用户
	 * <p>Function: getConstructionStaff</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21下午04:47:17 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public EiBlock getConstructionStaff(String _blockID, String _value, String _display, boolean isAll) {
		EiBlock block = new EiBlock(_blockID);

		EiColumn eiColumn = new EiColumn(_value);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn(_display);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		List<Map> _result = new ArrayList<Map>();
		if(isAll)  {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put(_value, "");
			_map.put(_display, "");
			_result.add(_map);
			
			for(int i = 0; i < _list.size(); i++) {
				_result.add(_list.get(i));
			}
		}
		else {
			_result = _list;
		}
		
		block.setRows(_result);
		return block;
	}
	
	/**
	 * 获取指定部门所有用户
	 * <p>Function: getDepartmentPeople</p>
	 * <p>Description: 根据传入的部门id得到部门人员</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-30 16:18:37 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public EiBlock getDepartmentPeople(String _blockID, String _value, String _display,String departCode, boolean isAll) {
		EiBlock block = new EiBlock(_blockID);

		EiColumn eiColumn = new EiColumn(_value);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn(_display);
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		List<Map> _result = new ArrayList<Map>();
		if(isAll)  {
			Map<String, String> _map = new HashMap<String, String>();
			_map.put(_value, "");
			_map.put(_display, "");
			_result.add(_map);
		}
		for(int i = 0; i < _list.size(); i++) {
			if(_list.get(i).get("orgCode")!=null && _list.get(i).get("orgCode").equals(departCode)){
				_result.add(_list.get(i));
			}
		}
		block.setRows(_result);
		return block;
	}
	
	/**
	 * 获取所有用户
	 * <p>Function: getConstructionStaff</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-21下午04:47:23 张小飞
	 * </p>
	 *
	 */
	public EiBlock getConstructionStaff(boolean isAll) {
		return getConstructionStaff("cmes.cs.constructionStaff", "empCode", "empName", isAll);
	}
	
	/**
	 * 初始化该类
	 * <p>Function: getInstance</p>
	 * <p>Description: </p>
	 * <p>return value:InitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-7上午07:55:35 严洪浩
	 * </p>
	 *
	 */
	public static User getInstance(){
		//第一次访问时status可能为0也可能为1
		if(CmesConstants.USER_STATUS == 1) {
			instance = new User();
		}
		return instance;
	}
}
